package dao;

import entry.Blog;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {

    // 得到所有的博客列表
    public List<Blog> getAllBlogs(){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            List<Blog> blogs = new ArrayList<Blog>();
            connection = JDBCUtil.getConnection();
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getString("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setPostTime(resultSet.getString("postTime"));
                blog.setUserId(resultSet.getInt("userId"));

                String content = resultSet.getString("content");
                String desc = content;
                if (content.length() > 40){
                    desc = content.substring(0, 41) + "...";
                }
                blog.setDesc(desc);
                blogs.add(blog);
            }
            return blogs;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    // 根据 blogId找对应博客
    public Blog getBlogById(int blogId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select * from blog where blogId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getString("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setPostTime(resultSet.getString("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setContent(resultSet.getString("content"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    // 根据用户名找博客
    public List<Blog> getBlogsByUser(int userId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            List<Blog> blogs = new ArrayList<Blog>();
            connection = JDBCUtil.getConnection();
            String sql = "select * from blog where userId=? order by postTime desc";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getString("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setPostTime(resultSet.getString("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                String content = resultSet.getString("content");
                blog.setContent(content);
                String desc = content;
                if (content.length() > 40){
                    desc = content.substring(0, 41) + "...";
                }
                blog.setDesc(desc);
                blogs.add(blog);
            }
            return blogs;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }


    // 插入博客
    public void insertBlog(Blog blog){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into blog values(null,'"+blog.getTitle()+"','"+blog.getContent()+"',"+blog.getUserId()+","+"now())";
            statement = connection.prepareStatement(sql);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    // 根据博客 ID删除博客
    public void deleteBlog(int blogId){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from blog where blogId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    // 根据博客 ID更新博客
    public void updateBlog(int blogId, Blog blog){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update blog set title='" + blog.getTitle() + "',content='" + blog.getContent()+"', postTime=now() where blogId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    // 根据关键字查找博客
    public List<Blog> findBlogByWord(String word){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            List<Blog> blogs = new ArrayList<Blog>();
            connection = JDBCUtil.getConnection();
            String sql = "select * from blog where title like '%"+word+"%'";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getString("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setPostTime(resultSet.getString("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                String content = resultSet.getString("content");
                blog.setContent(content);
                String desc = content;
                if (content.length() > 40){
                    desc = content.substring(0, 41) + "...";
                }
                blog.setDesc(desc);
                blogs.add(blog);
            }
            return blogs;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }
}
